package com.t2c.tibetantochinese.fivehundred;

/**
 * Created by zoulu on 16/12/5.
 */


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.t2c.tibetantochinese.R;
import com.t2c.tibetantochinese.base.BaseFragment;
import com.t2c.tibetantochinese.home.HomeFragment;
import com.t2c.tibetantochinese.search.SearchAdapter;
import com.t2c.tibetantochinese.search.SearchAdapterSimple;
import com.t2c.tibetantochinese.utils.CommonUtil;
import com.t2c.tibetantochinese.utils.RealmUtil;

/**
 * Created by zoulu on 16/6/21.
 * 搜索结果页
 */
public class FiveHundredDetailFragment extends BaseFragment {
    private ListView listView;
    private TextView tv_pop;
    private TextView cancel;
    private EditText edittext;
    private ImageView iv_cancel;
    private int postion;

    @Override
    protected int getContentViewResId() {
        return R.layout.searchresult_layout;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init(view);
        initData();
    }

    private void init(View view) {
        int visiable = getArguments().getInt("visiable");
        postion = getArguments().getInt("POS");
        listView = (ListView) view.findViewById(R.id.listview);
        tv_pop = (TextView) view.findViewById(R.id.tv_pop);
        cancel = (TextView) view.findViewById(R.id.cancel);
        edittext = (EditText) view.findViewById(R.id.edittext);
        iv_cancel = (ImageView) view.findViewById(R.id.iv_cancel);
        switch (visiable) {
            case View.GONE:
                tv_pop.setVisibility(View.GONE);
                break;
            case View.VISIBLE:
                tv_pop.setVisibility(View.VISIBLE);
                break;
            default:
                tv_pop.setVisibility(View.GONE);
                break;
        }
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommonUtil.hideKeyboard(getActivity());
                HomeFragment homeFragment = CommonUtil.getHomeFragment(getActivity());
                homeFragment.fragmentBack();
            }
        });
        iv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edittext.setText("");
            }
        });

        edittext.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
                if (arg1 == EditorInfo.IME_ACTION_SEARCH) {
                    if (TextUtils.isEmpty(edittext.getText().toString())) {
                        Toast.makeText(getActivity(), "请输入要搜索的内容", Toast.LENGTH_SHORT).show();
                    } else {
                        SearchAdapterSimple searchAdapterSimple = new SearchAdapterSimple(getActivity(), RealmUtil.getPhraseByName(edittext.getText().toString()));
                        listView.setAdapter(searchAdapterSimple);
                        CommonUtil.hideKeyboard(getActivity());
                    }
                }
                return false;
            }

        });
    }

    private void initData() {
        if (postion != -1) {
            SearchAdapter searchAdapter = new SearchAdapter(getActivity(), RealmUtil.getPhraseByCategoryUuid(postion));
            listView.setAdapter(searchAdapter);
        }
    }
}
